export default {
  data () {
    return {
      totalPowerNum: 0, //权限总数
      isAllCheck: false, //全选-反选
      checkedSum: 0, //选中的项数
      dialogVisibleForRole: false, //新增角色
      tabIndexForRole: 1, //(1:创建角色;2:设置权限)
      addRoleForm:{id:"",name:"",remark:""},
      roleRules:{
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'change' }
        ],
        remark: [
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'change' }
        ]
      },
      roleForm:{id:"",name:"",remark:""},
      roleId:"",
      rules:{
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'change' }
        ],
        remark: [
          // { required: true, message: '请输入描述', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    
  },
  methods: {
    checkAllOrNo(){ //全选-反选方法
      let treeObj = $.fn.zTree.getZTreeObj("privilegeTree");
      if(this.isAllCheck==true){
        treeObj.checkAllNodes(true);
      }else{
        treeObj.checkAllNodes(false);
      }
      this.checkedSum = treeObj.getCheckedNodes(true).length;
    },
    toOpenAddRoleDialog(){
      this.addRoleForm.name = ''
      this.addRoleForm.remark = ''
      this.dialogVisibleForRole=true;
      this.initParkTreeForPower('privilegeTree')
    },
    initParkTreeForPower (treeId) {//新增角色-初始化权限树
      let self = this
      self.isAllCheck = false
      self.checkedSum = 0
      API.platform_manage.FindResourceForSetup({}).then(res => {
        $.fn.zTree.destroy(treeId);
        var settings = {
          check: {
              enable: true
          },
          data: {
              simpleData: {
                  enable: true
              }
          },
          view: {
              showIcon: false,
              showLine: false,
          },
          callback: {
            onCheck: self.zTreeOnCheck
          }
        };
        $.fn.zTree.init($("#"+treeId), settings, res.data);
        self.totalPowerNum = res.data.length
        let count = 0
        for(let i = 0; i<res.data.length; i++){
          let obj = res.data[i]
          count = obj.checked==true ? count+1 : count+0
        }
        self.checkedSum = count
      },err => {});
    },
    setPrivilegeForAddRole (treeId){ //新增角色-获取选取的权限数组
      var self = this;
      var tree = $.fn.zTree.getZTreeObj(treeId);
      var nodes = tree.getCheckedNodes(true);
      var parkList = [];
      for (var i = 0; i < nodes.length; i++) {
          if (!nodes[i].isArea) {
              parkList.push(nodes[i].id);
          }
      }
      if (parkList.length <= 0) {
          self.showToast('请至少选择一个权限', 0);
          return false;
      }
      return parkList;
    },
    saveAddRoleModal(formName){//新增角色-保存角色
      let self = this
      this.$refs[formName].validate((valid) => {
          if (!valid) {
            return false;
          }else{
            let params = $.extend({},this.addRoleForm)
            let resourceIdList = this.setPrivilegeForAddRole('privilegeTree')
            params.resourceIdList = resourceIdList
            API.platform_manage.SaveRoleData(params).then(res =>{
              if(res.status==1){
                this.showToast("操作成功",1);
                this.dialogVisibleForRole = false
                //获取角色列表
                self.getList()
              }else{
                this.showToast("操作失败",2);
              }
            }, err => {});
          }
      });
    },
    getList () {
      this.getRoleList(this.searchParams);
    },
    getRoleList (aparams) {//查询列表
      let self = this,
          params = {
            page: this.pageNumber,
            rp: this.pageSize
          };
      params = $.extend({}, params, aparams);
      API.platform_manage.GetRoleListData(params)
          .then(res => {
            let data = res.data,
                list = data.rows;
            self.tbodysInfo = list;
            self.total = data.total;
          }, err => {})
    },
    toOpenAddRole (modalName) {//打开新增
      this.roleForm.id = "";
      this.roleForm.name = "";
      this.roleForm.remark = "";
      this.toOpenModal(modalName);
    },
    toOpenEditRole (row){//打开编辑
      this.roleForm.id = row.id;
      this.roleForm.name = row.name;
      this.roleForm.remark = row.remark;
      this.toOpenModal('roleRowModal');
    },
    toOpenPrivilege(row){//打开设置权限对话框
      let self = this;
      self.roleId = row.id;
      self.toOpenModal('setPrivilegeModal',1);
      self.initParkTree("privilegeTree", row.id);
    },
    initParkTree (treeId,roleID) {//初始化权限树
      let self = this
      self.isAllCheck = false
      self.checkedSum = 0
      API.platform_manage.FindResourceForSetup({"roleID":roleID}).then(res => {
        $.fn.zTree.destroy(treeId);
        var settings = {
          check: {
              enable: true
          },
          data: {
              simpleData: {
                  enable: true
              }
          },
          view: {
              showIcon: false,
              showLine: false,
          },
          callback: {
            onCheck: self.zTreeOnCheck
          }
        };
        $.fn.zTree.init($("#"+treeId), settings, res.data);
        self.totalPowerNum = res.data.length
        let count = 0
        for(let i = 0; i<res.data.length; i++){
          let obj = res.data[i]
          count = obj.checked==true ? count+1 : count+0
        }
        self.checkedSum = count
      },err => {});
    },
    zTreeOnCheck(){
      let treeObj = $.fn.zTree.getZTreeObj("privilegeTree");
      this.checkedSum = treeObj.getCheckedNodes(true).length;
      if(this.checkedSum==this.totalPowerNum){
        this.isAllCheck = true
      }else{
        this.isAllCheck = false
      }
    },
    SetPrivilege (treeId){//保存权限
      var self = this;
      var tree = $.fn.zTree.getZTreeObj(treeId);
      var nodes = tree.getCheckedNodes(true);
      var resources = [];
      for (var i = 0; i < nodes.length; i++) {
          resources.push(nodes[i].id);
      }
      var params = {
          'id': self.roleId,
          'resourceIdList': resources
      };
      API.platform_manage.SetPrivilege(params).then(res => {
          if(res.status==1){
            self.showToast("操作成功",1);
            self.closed('setPrivilegeModal');
            self.getList();
          }else{
            self.showToast("操作失败",2);
          }
      }, err => {});
    },
    filterMultiCheck (selectionArr) {//必须定义获取多列的方法
      let result = [];
        selectionArr.map((item)=>{
          result.push(item.id);
        });
      return result;
    },
    toOpenDeleteRole(row){//打开删除对话框
      let self = this;
      let ids = [];
      if(row){
        //单行
        ids.push(row.id);
      }else{
        //判断是否选择了多行
        let idsStr = this.selectionList;
        if(idsStr<=0){
          this.showToast("请至少选择一项",0);
          return false;
        }else{
          ids = idsStr.split(",");
        }
      }

      this.showDialog("是否确认删除所选记录？",[],function(){
        API.platform_manage.DeleteBatchRole({"JSONSTRING":JSON.stringify(ids)}).then(res => {
          if(res.status==1){
            self.getList();
            self.showToast("操作成功",1);
          }else{
            self.showToast("操作失败",2);
          }
        }, err =>{});
      },"温馨提示",function(){
        //取消时不操作
      });
    },
    saveRole(formName){//保存角色
      this.$refs[formName].validate((valid) => {
          if (!valid) {
            return false;
          }else{
            API.platform_manage.SaveRoleData(this.roleForm).then(res =>{
              if(res.status==1){
                this.closed('roleRowModal');
                this.getList();
                this.showToast("操作成功",1);
              }else{
                this.showToast("操作失败",2);
              }
            }, err => {});
          }
      });
    }
  }
}